import UnityEngine

class Manager (MonoBehaviour): 
    public healer as Transform
    playerid as NetworkViewID
    respawn as List
    def OnEnable ():
        if Network.isServer:
            # viewid = Network.AllocateViewID()
            # networkView.RPC("Spawn", RPCMode.AllBuffered, viewid, Vector3(10, 1, 10))
            Spawn(Vector3(10,1,10))
            # EnableControls(viewid)
            
            respawn = List()
    
    def Update():
        if Network.isServer:
            remove = List()
            for player as NetworkPlayer, time as double in respawn:
                if time > Time.time:
                    if player != Network.player:
                        networkView.RPC("Spawn", player, Vector3(10,1,10))
                    else:
                        Spawn(Vector3(10,1,10))
                    remove.Add((player, time))
        
            for r in remove:
                respawn.Remove(r)
    
    def OnPlayerConnected(player as NetworkPlayer):
        # Spawn a player for them
        networkView.RPC("Spawn", player, Vector3(10,1,10))
    
    def OnPlayerDisconnected(player as NetworkPlayer):
        Network.DestroyPlayerObjects(player)
    
    def Respawn(player as NetworkPlayer):
        if Network.isServer:
            Network.RemoveRPCs(player)
            respawn.Add((player, Time.time + 3))
    
    [RPC]
    def Spawn(position as Vector3):
        obj as Transform = Network.Instantiate(healer, position, Quaternion.identity, 0)
        EnableControls(obj)
        # obj = GameObject.Instantiate(healer, position, Quaternion.identity)
        # obj.networkView.viewID = viewid
    
    [RPC]
    def EnableControls(trans as Transform):
        # view = NetworkView.Find(viewid)
        trans.GetComponent(PlayerControls).enabled = true
        for child as Transform in trans:
            if child.gameObject.name == "Camera":
                child.camera.enabled = true
    

